package com.ruoyi.develop.lvYifahuomingxi.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lvFahuomingxi.domain.LvFahuomingxi;
import com.ruoyi.develop.lvFahuomingxi.mapper.LvFahuomingxiMapper;
import com.ruoyi.develop.lvFahuotongzhi.service.ILvFahuotongzhiService;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.impl.LvRukumingxiServiceImpl;
import com.ruoyi.develop.lvYifahuo.domain.LvYifahuo;
import com.ruoyi.develop.lvYifahuo.mapper.LvYifahuoMapper;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvYifahuomingxi.mapper.LvYifahuomingxiMapper;
import com.ruoyi.develop.lvYifahuomingxi.domain.LvYifahuomingxi;
import com.ruoyi.develop.lvYifahuomingxi.service.ILvYifahuomingxiService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货单明细Service业务层处理
 * @author qh
 * @date 2024-10-08
 */
@Service
public class LvYifahuomingxiServiceImpl implements ILvYifahuomingxiService {
    @Resource
    private LvYifahuomingxiMapper lvYifahuomingxiMapper;
    @Resource
    private LvFahuomingxiMapper lvFahuomingxiMapper;
    @Resource
    private LvYifahuoMapper lvYifahuoMapper;
    @Resource
    private LvRukumingxiServiceImpl lvRukumingxiService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;
    @Autowired
    private ILvFahuotongzhiService iLvFahuotongzhiService;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;

    /**
     * 查询发货单明细
     * @param id 发货单明细主键
     * @return 发货单明细
     */
    @Override
    public LvYifahuomingxi selectById(String id) {
        return lvYifahuomingxiMapper.selectById(id);
    }

    @Override
    public LvYifahuomingxi selectWeikaMoney(String status) {
        return lvYifahuomingxiMapper.selectWeikaMoney(status);
    }

    @Override
    public LvYifahuomingxi selectWeikaMoneyQishen(String fahuoId) {
        return lvYifahuomingxiMapper.selectWeikaMoneyQishen(fahuoId);
    }
    /**
     * 查询发货单明细列表
     * @param entity 发货单明细
     * @return 发货单明细
     */
    @Override
    public List<LvYifahuomingxi> selectList(LvYifahuomingxi entity) {
        return lvYifahuomingxiMapper.selectList(entity);
    }

    @Override
    public List<LvYifahuomingxi> selectListMerge(LvYifahuomingxi entity) {
        return lvYifahuomingxiMapper.selectListMerge(entity);
    }



    @Override
    public List<LvYifahuomingxi> listNoCommit(LvYifahuomingxi entity) {
        return lvYifahuomingxiMapper.listNoCommit(entity);
    }

    @Override
    public List<LvYifahuomingxi> listNoFahuo(LvYifahuomingxi entity) {
        return lvYifahuomingxiMapper.listNoFahuo(entity);
    }
    @Override
    public List<LvYifahuomingxi> selectByFahuoIdGrouByProid(String fahuoId) {
        return lvYifahuomingxiMapper.selectByFahuoIdGrouByProid(fahuoId);
    }
    @Override
    public List<LvYifahuomingxi> selectByTogzhiId(String tongzhiId) {
        return lvYifahuomingxiMapper.selectByTogzhiId(tongzhiId);
    }
    @Override
    public List<LvYifahuomingxi> selectByFahuoId(String fahuoId) {
        return lvYifahuomingxiMapper.selectByFahuoId(fahuoId);
    }
    @Override
    public List<LvYifahuomingxi> selectByFahuoIdDistinctProNo(String fahuoId) {
        return lvYifahuomingxiMapper.selectByFahuoIdDistinctProNo(fahuoId);
    }
    @Override
    public List<LvYifahuomingxi> selectByFahuoIdDistinctProNoZancun() {
        return lvYifahuomingxiMapper.selectByFahuoIdDistinctProNoZancun();
    }
    /**
     * 新增发货单明细
     * @param entity 发货单明细
     * @return 结果
     */
    @Override
    public int insert(LvYifahuomingxi entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return lvYifahuomingxiMapper.insert(entity);
    }
    @Override
    @Transactional
    public void insertBatch(LvYifahuomingxi fahuomingxi) {
        List<LvFahuomingxi> rows = fahuomingxi.getColumns();
        if (!rows.isEmpty()) {
            for (LvFahuomingxi entity : rows) {
                LvYifahuomingxi lvFahuomingxi=new LvYifahuomingxi();
                lvFahuomingxi.setProId(entity.getProId());
                lvFahuomingxi.setProNo(entity.getProNo());
                lvFahuomingxi.setProdName(entity.getProdName());
                lvFahuomingxi.setSaleId(entity.getSaleId());
                lvFahuomingxi.setGoodsId(entity.getGoodsId());
                lvFahuomingxi.setGoodsType(entity.getGoodsType());
                lvFahuomingxi.setDanwei(entity.getDanwei());
                lvFahuomingxi.setDanjian(entity.getDanjian());
                lvFahuomingxi.setName(entity.getName());
                lvFahuomingxi.setModels(entity.getModels());
                lvFahuomingxi.setMingxi(entity.getMingxi());
                lvFahuomingxi.setNos(entity.getNos());
                lvFahuomingxi.setShuliang(entity.getShengyuCount());
                lvFahuomingxi.setMoney(entity.getShuliang().multiply(entity.getDanjian()));
                lvFahuomingxi.setRemark(entity.getRemark());
                lvFahuomingxi.setTongzhiId(entity.getId());
                lvFahuomingxi.setTongzhiNo(entity.getNos());
                lvFahuomingxi.setSaleman(entity.getOrder().getSalesmanName());
                if("3".equals(fahuomingxi.getStatus())){
                    lvFahuomingxi.setStatus(BusinessConstants.YIFAHUO_3);
                    lvFahuomingxi.setFahuoId(fahuomingxi.getFahuoId());
                    lvFahuomingxi.setFahuoNo(fahuomingxi.getFahuoNo());
                }
                lvFahuomingxi.setTzId(fahuomingxi.getTzId());
                lvFahuomingxi.setTzNo(fahuomingxi.getTzNo());
                insert(lvFahuomingxi);
                entity.setFahuoCount(entity.getFahuoCount().add(entity.getShengyuCount()));
                entity.setShengyuCount(entity.getShuliang().subtract(entity.getFahuoCount()));
                if(entity.getFahuoCount().compareTo(entity.getShuliang())==0){
                    entity.setStatus(BusinessConstants.FAHUO_2);
                    entity.setIsFahuo(BusinessConstants.FAHUO_2);
                }
                lvFahuomingxiMapper.update(entity);
            }
        }
    }
    @Override
    @Transactional
    public void updateForFh(LvYifahuomingxi fahuomingxi) {
        List<LvFahuomingxi> rows = fahuomingxi.getColumns();
        if (!rows.isEmpty()) {
            for (LvFahuomingxi entity : rows) {
                LvYifahuomingxi lvFahuomingxi=new LvYifahuomingxi();
                lvFahuomingxi.setProId(entity.getProId());
                lvFahuomingxi.setProNo(entity.getProNo());
                lvFahuomingxi.setProdName(entity.getProdName());
                lvFahuomingxi.setSaleId(entity.getId());
                lvFahuomingxi.setDanwei(entity.getDanwei());
                lvFahuomingxi.setDanjian(entity.getDanjian());
                lvFahuomingxi.setName(entity.getName());
                lvFahuomingxi.setModels(entity.getModels());
                lvFahuomingxi.setMingxi(entity.getMingxi());
                lvFahuomingxi.setNos(entity.getNos());
                lvFahuomingxi.setShuliang(entity.getShengyuCount());
                lvFahuomingxi.setMoney(entity.getShuliang().multiply(entity.getDanjian()));
                lvFahuomingxi.setRemark(entity.getRemark());
                lvFahuomingxi.setTongzhiId(entity.getId());
                lvFahuomingxi.setTongzhiNo(entity.getNos());
                insert(lvFahuomingxi);
                entity.setFahuoCount(entity.getFahuoCount().add(entity.getShengyuCount()));
                entity.setShengyuCount(entity.getShuliang().subtract(entity.getFahuoCount()));
                if(entity.getFahuoCount().compareTo(entity.getShuliang())==0){
                    entity.setStatus(BusinessConstants.FAHUO_2);
                    entity.setIsFahuo(BusinessConstants.FAHUO_2);
                }
                lvFahuomingxiMapper.update(entity);
            }
        }
    }
    /**
     * 修改发货单明细
     * @param entity 发货单明细
     * @return 结果
     */
    @Override
    public int update(LvYifahuomingxi entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return lvYifahuomingxiMapper.update(entity);
    }
    /**
     * 批量删除发货单明细
     * @param ids 需要删除的发货单明细主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvYifahuomingxiMapper.deleteByIds(ids);
    }
    @Override
    public void deleteByFahuoId(String fahuoId) {
        lvYifahuomingxiMapper.deleteByFahuoId(fahuoId);
    }
    /**
     * 删除发货单明细信息
     * @param id 发货单明细主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvYifahuomingxiMapper.deleteById(id);
    }
    @Override
    public int updateDate(String tzId,String fahuoId,Date fahuoDate,Date jiaohuoDate) {
        return lvYifahuomingxiMapper.updateDate(tzId,fahuoId, fahuoDate, jiaohuoDate);
    }
    @Override
    public int updateByFahuoId(String tzId,String fahuoId,String fahuoNo,String isTijiao) {
        return lvYifahuomingxiMapper.updateByFahuoId(tzId,fahuoId,fahuoNo,isTijiao);
    }
    @Override
    public int updateByFahuoIdQishen(String fahuoId,String isTijiao) {
        return lvYifahuomingxiMapper.updateByFahuoIdQishen(fahuoId,isTijiao);
    }
    @Override
    public int updateStatusByFahuoId(String fahuoId,String status) {
       // iLvFahuotongzhiService.updateState(lvYifahuo.getTongzhiId(), BusinessConstants.FAHUOTONGZHI_2);
        //更改发货明细状态
        List<LvYifahuomingxi> lvYifahuomingxiList = lvYifahuomingxiMapper.selectByFahuoId(fahuoId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(new Date());
        for (LvYifahuomingxi lvYifahuomingxi : lvYifahuomingxiList) {
            //加库存
            GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(lvYifahuomingxi.getGoodsId());
            //新增出库单产品明细
            LvRukumingxi lvRukumingxi = new LvRukumingxi();
            ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(lvYifahuomingxi.getProId());
            GoodsSale goodsSale = goodsSaleService.selectById(lvYifahuomingxi.getSaleId());
            if (projectBaseInfo != null) {
                if (StringUtils.isNotBlank(projectBaseInfo.getCustomId())) {
                    lvRukumingxi.setCustomerId(projectBaseInfo.getCustomId());
                }
                if (StringUtils.isNotBlank(projectBaseInfo.getCustomName())) {
                    lvRukumingxi.setCustomerName(projectBaseInfo.getCustomName());
                }
                lvRukumingxi.setNos(time);
                lvRukumingxi.setSaleId(projectBaseInfo.getSaleId());
                lvRukumingxi.setSaleName(projectBaseInfo.getSalesmanName());
                lvRukumingxi.setGoodsTotalPrice(projectBaseInfo.getSaleMoney());
                lvRukumingxi.setProjectId(projectBaseInfo.getId());
                lvRukumingxi.setProjectName(projectBaseInfo.getName());
                lvRukumingxi.setProjectNo(projectBaseInfo.getCode());
                lvRukumingxi.setGoodsId(goodsOrderIn.getId());
                lvRukumingxi.setCode(goodsOrderIn.getCode());
                lvRukumingxi.setGoodsType(BusinessConstants.GOODS_TYPE_MATERIAL);//产品类型
                lvRukumingxi.setType(BusinessConstants.TYPR_CHUKU);//产品出入库类型
                lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
                //lvRukumingxi.setGoodsBuyPrice(lvYifahuomingxi.getDanjian());//发货成本单件
                //lvRukumingxi.setGoodsBuyTotal(lvYifahuomingxi.getMoney());//发货成本总价
                //lvRukumingxi.setGoodsProfit(goodsSale.getGoodsTotalPrice().subtract(lvYifahuomingxi.getMoney()));//利润=销售价-成本
                lvRukumingxi.setDaohuoCount(lvYifahuomingxi.getShuliang());//出入库数量
                if (BusinessConstants.GOODS_TYPE_MATERIAL.equals(lvYifahuomingxi.getGoodsType())) {
                    lvRukumingxi.setDun(goodsSale.getCount());//到货吨位
                }
                lvRukumingxi.setUnit(lvYifahuomingxi.getDanwei());
                lvRukumingxi.setRukudate(new Date());
                lvRukumingxi.setRukuno(lvYifahuomingxi.getFahuoNo());
                lvRukumingxi.setRemark("发货弃审："+lvYifahuomingxi.getRemark());
                lvRukumingxi.setGoodsSaleId(goodsSale.getId());
                lvRukumingxiService.insert(lvRukumingxi);

                //更新销售订单的产品
//                goodsSale.setFahuoCount(goodsSale.getFahuoCount().subtract(lvYifahuomingxi.getShuliang()));
//                goodsSale.setFahuoShengyu(goodsSale.getFahuoShengyu().add(lvYifahuomingxi.getShuliang()));
                 /*if(goodsSale.getFahuoShengyu().compareTo(BigDecimal.ZERO)){
                    goodsSale.setStatus(BusinessConstants.SALE_TYPE_7);//全部发货
                }else{*/
                goodsSale.setStatus(BusinessConstants.SALE_TYPE_11);//发货中
                /*}*/
                //goodsSale.setGoodsBuyPrice(lvYifahuomingxi.getDanjian());//发货成本单件
                //goodsSale.setGoodsBuyTotal(lvYifahuomingxi.getMoney());//发货成本总价
                //goodsSale.setGoodsProfit(projectBaseInfo.getSaleMoney().subtract(lvYifahuomingxi.getMoney()));//利润=销售价-成本
                goodsSaleService.update(goodsSale);

                //更新销售订单的利润
                projectBaseInfo.setStatus(BusinessConstants.SALE_TYPE_11);
                //projectBaseInfo.setGoodsBuyPrice(lvYifahuomingxi.getDanjian());//发货成本单件
                //projectBaseInfo.setGoodsBuyTotal(lvYifahuomingxi.getMoney());//发货成本总价
                //projectBaseInfo.setGoodsProfit(projectBaseInfo.getSaleMoney().subtract(lvYifahuomingxi.getMoney()));//利润=销售价-成本
                projectBaseInfoService.update(projectBaseInfo);

                //更新库存数量 和剩余库存数量
                // 1.发多少货 库存就减去多少  2.可用数量 减少量 = 发货总数量-锁定数量
                goodsOrderIn.setCount(goodsOrderIn.getCount().add(lvYifahuomingxi.getShuliang()));//库存数量=库存数量+弃审数量

                BigDecimal lockCount = goodsSale.getLockCount();
                // 该订单产品 发货总数量
                BigDecimal goodsSendTotal = lvYifahuomingxiMapper.goodsSendTotalCount(goodsSale.getId());
                if(goodsSendTotal.subtract(lockCount).compareTo(BigDecimal.ZERO) > 0){
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(goodsSendTotal.subtract(lockCount)));
                }
                goodsOrderInMapper.update(goodsOrderIn);
            } else {
                return 0;
            }

        }
        //更改发货货单明细
        return lvYifahuomingxiMapper.updateStatusByFahuoId(fahuoId,status);
    }

    @Override
    public void insertSendDetailFormGoodsSale(LvYifahuomingxi entity) {
        List<GoodsSale> goodsSaleList = entity.getGoodsSaleList();
        if (!goodsSaleList.isEmpty()) {
            for (GoodsSale goodsSale : goodsSaleList) {
                LvYifahuomingxi sendDetail=new LvYifahuomingxi();
                sendDetail.setProId(goodsSale.getProjectId());
                sendDetail.setProNo(goodsSale.getProjectNo());
                sendDetail.setProdName(goodsSale.getProjectName());
                sendDetail.setSaleId(goodsSale.getId());
                sendDetail.setGoodsId(goodsSale.getGoodsId());
                sendDetail.setGoodsType(goodsSale.getGoodsType());
                sendDetail.setDanwei(goodsSale.getUnit());
                sendDetail.setDanjian(goodsSale.getGoodsTaxUnitPrice());
                sendDetail.setName(goodsSale.getGoodsName());
                sendDetail.setModels(goodsSale.getGoodModel());
                sendDetail.setMingxi(goodsSale.getBrand());
                sendDetail.setNos(goodsSale.getGoodsCode());
                sendDetail.setFahuoId(entity.getFahuoId());
                sendDetail.setShuliang(goodsSale.getSendCount());
                sendDetail.setMoney(goodsSale.getSendCount().multiply(goodsSale.getGoodsTaxUnitPrice()));
                sendDetail.setRemark(entity.getRemark());
                sendDetail.setSaleman(goodsSale.getOrder().getSalesmanName());
                sendDetail.setKehuId(goodsSale.getCustomerId());
                sendDetail.setKehuName(goodsSale.getCustomerName());
                insert(sendDetail);
                goodsSale.setFahuoCount(goodsSale.getFahuoCount().add(goodsSale.getSendCount()));
                goodsSale.setFahuoShengyu(goodsSale.getFahuoShengyu().subtract(goodsSale.getSendCount()));
                if(goodsSale.getFahuoShengyu().compareTo(BigDecimal.ZERO)==0){
                    // TODO
                }
                goodsSaleService.update(goodsSale);
            }
        }
    }

    @Override
    public BigDecimal selectFahuoCountBuySaleId(String saleId) {
        return lvYifahuomingxiMapper.selectFahuoCountBuySaleId(saleId);
    }


}
